<?php
//
// +---------------------------------------------------------------------------------+
// | Package: EezeeSite Core                                                         |
// +---------------------------------------------------------------------------------+
// | Copyright (C) 2006  EezeeSite Development Team                                  |
// +---------------------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or                   |
// | modify it under the terms of the GNU General Public License                     |
// | as published by the Free Software Foundation; either version 2                  |
// | of the License, or (at your option) any later version.                          |
// |                                                                                 |
// | This program is distributed in the hope that it will be useful,                 |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of                  |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                   |
// | GNU General Public License for more details.                                    |
// |                                                                                 |
// | You should have received a copy of the GNU General Public License               |
// | along with this program; if not, write to the Free Software                     |
// | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. |
// +---------------------------------------------------------------------------------+
// | Author: Mayukh De <mayukhde@gmail.com>                                          |
// +---------------------------------------------------------------------------------+
//

class ESSystemClientCheckModeAJAX extends ESSystemClientCheck
{   
    function initESClient()
    {
        global $ESResponse;
        $this->clientSession();
        if ($_SESSION['ESSys'] == '1') {
            return;
        } else {
            foreach ($this->clientCheckList as $k => $v) {
                if (method_exists($this, $v.'Check')) {
                    //$this->AJAXResponse->insertScript("showSplash();");
                    return call_user_func(array(&$this, $v.'Check'));
                    //$this->AJAXResponse->insertScript("hideSplash();");
                } else {
                    $ESResponse->raiseESError(sprintf($GLOBALS['ES']['LANG']['SYSERROR']['UNHANDLED'], $v, 'Check'), 'log');
                    $this->AJAXResponse->insertAlert("An error has occurred in the application-\n".sprintf($GLOBALS['ES']['LANG']['SYSERROR']['UNHANDLED'], $v, 'Check'));
                }
            }
            $this->clientSession(1);
        }
        return $this->AJAXResponse;
    }

    function configCheck()
    {
        if (!file_exists(ES_CONFIG_FILE)) {
            //$this->response->raiseESError($GLOBALS['ES']['LANG']['INSTALLPROMPT'], 'dieOn');
            $this->AJAXResponse->insertAlert($GLOBALS['ES']['LANG']['INSTALLPROMPT']);
            $this->AJAXResponse->insertScript("window.location = '".ES_BASE_URL."admin".ES_FILE_EXTN."?mode=install';");
            return $this->AJAXResponse;
        } else {
            return;
        }
    }
    
    function DBCheck()
    {
        global $ESDB;
        if (!$ESDB || PEAR::isError($ESDB)) {
            //$this->response->raiseESError($GLOBALS['ES']['LANG']['INSTALLPROMPT'], 'dieOn');
            $this->AJAXResponse->insertAlert($GLOBALS['ES']['LANG']['INSTALLPROMPT']);
            $this->AJAXResponse->insertScript("window.location = '".ES_BASE_URL."admin".ES_FILE_EXTN."?mode=install';");
            return $this->AJAXResponse;
        } else {
            return;
        }
    }
    
    function tablesCheck()
    {
        global $ESDB;
        global $DBSchemaConfigs;
        $sql = 'SHOW TABLES LIKE \''.ES_TBL_PRE.'%\';';
        $result = $ESDB->query($sql);
        $numTables = $result->numRows();
        if ($numTables == 0) {
            //$this->response->raiseESError($GLOBALS['ES']['LANG']['INSTALLPROMPT'], 'dieOn');
            $this->AJAXResponse->insertAlert($GLOBALS['ES']['LANG']['INSTALLPROMPT']);
            $this->AJAXResponse->insertScript("window.location = '".ES_BASE_URL."admin".ES_FILE_EXTN."?mode=install';");
            return $this->AJAXResponse;
        } elseif ($numTables != 0 && $numTables < count($DBSchemaConfigs['TABLES'])) {
            $this->response->raiseESError('upgrade needed', 'dieOn');
        } else {
            return;
        }
    }
    
    function tplsCheck()
    {
        if ($GLOBALS['ES']['NEWTPLS'] && is_array($GLOBALS['ES']['NEWTPLS'])) {
            $this->response->raiseESError('upgrade needed', 'dieOn');
        } else {
            return;
        }
    }
    
    function htaccessCheck()
    {
        if (!file_exists(ES_INSTALL_DIR.'/.htaccess')&& $GLOBALS['ES']['CLIENTCONF']['rewrite_url'] = '1') {
            //$this->response->raiseESError($GLOBALS['ES']['LANG']['INSTALLPROMPT'], 'dieOn');
            $this->AJAXResponse->insertAlert($GLOBALS['ES']['LANG']['INSTALLPROMPT']);
            $this->AJAXResponse->insertScript("window.location = '".ES_BASE_URL."admin".ES_FILE_EXTN."?mode=install';");
            return $this->AJAXResponse;
        } else {
            return;
        }        
    }
    
    function installLockCheck()
    {
        if (!file_exists($_SERVER['DOCUMENT_ROOT'].'/../inc/'.md5(ES_BASE_URL).'.lock')) {
            //if (isset($_GET['sysCheck']) && $_GET['sysCheck'] = 'all') {
                //$this->response->raiseESError($GLOBALS['ES']['LANG']['INSTALLPROMPT'], 'dieOn');
                $this->AJAXResponse->insertAlert($GLOBALS['ES']['LANG']['INSTALLPROMPT']);
                $this->AJAXResponse->insertScript("window.location = '".ES_BASE_URL."admin".ES_FILE_EXTN."?mode=install';");
                return $this->AJAXResponse;
            //} else {
                //$this->response->sendHTTPHeader('REDIRECT', array('LOC' => ES_BASE_URL.'?sysCheck=all'));
            //}
        } else {
            //define('ES_INSTALLED', true);
            return;
        }
    }
}

?>